//
//  HomePageViewController.m
//  
//
//  Created by dllo on 16/8/24.
//  Copyright © 2016年 高雅馨. All rights reserved.
//

#import "HomePageViewController.h"
#import "MatchViewController.h"
#import "DiscoverViewController.h"
#import "UIView+Extension.h"
#import "Macro.h"
@interface HomePageViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UICollectionView *homeCollectionView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIViewController *currentViewController;
// navigationBar的自定义titleView
@property (nonatomic, strong) UIView *titleView;
// titleArray
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIButton *selectedButton;

@end

@implementation HomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 创建子视图控制器
    [self createChildViewController];
    // 创建titleView
    self.titleArray = [NSArray arrayWithObjects:@"速配聊天", @"发现", nil];
    [self createTitleView];
    // 创建scrollView
    [self createScrollView];
    // 创建barButton
    [self createBarButton];
}
- (void)createChildViewController {
    MatchViewController *match = [[MatchViewController alloc]init];
    DiscoverViewController *discover = [[DiscoverViewController alloc] init];
    [self addChildViewController:match];
    [self addChildViewController:discover];
}
- (void)createTitleView {
    self.titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    self.lineView = [[UIView alloc] init];
    _lineView.backgroundColor = [UIColor  blackColor];
    _lineView.height = 3;
    for (int i = 0; i < _titleArray.count; i++) {
        UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        titleButton.frame = CGRectMake(_titleView.frame.size.width / 2 * i, 0, _titleView.frame.size.width / 2, _titleView.frame.size.height - 3);
        [self.titleView addSubview:titleButton];
        [titleButton setTitle:_titleArray[i] forState:UIControlStateNormal];
        [titleButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateDisabled];
        titleButton.tag = 1000 + i;
        titleButton.titleLabel.font = [UIFont systemFontOfSize:17];
        [titleButton addTarget:self action:@selector(didClickedTitleButton:) forControlEvents:UIControlEventTouchUpInside];
        _lineView.y = titleButton.frame.size.height;
        if (i == 0) {
            //   让按钮的lable根据内容计算尺寸
            [titleButton.titleLabel sizeToFit];
            
            [self didClickedTitleButton:titleButton];
            _lineView.width = titleButton.titleLabel.width;
            _lineView.centerX = titleButton.center.x;
        }
    }
    [self.titleView addSubview:_lineView];
    self.navigationItem.titleView = _titleView;
}
// 创建ScrollView
- (void)createScrollView {
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49)];
    _scrollView.contentSize = CGSizeMake(self.childViewControllers.count * SCREEN_WIDTH, 0);
    _scrollView.delegate = self;
    _scrollView.pagingEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_scrollView];
    [self.view insertSubview:_scrollView atIndex:0];
    [self scrollViewDidEndScrollingAnimation:_scrollView];
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    NSInteger i = scrollView.contentOffset.x / SCREEN_WIDTH;
    self.currentViewController = self.childViewControllers[i];
    _currentViewController.view.frame = CGRectMake(scrollView.contentOffset.x, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 44);
    [scrollView addSubview:_currentViewController.view];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self scrollViewDidEndScrollingAnimation:scrollView];
    NSInteger index = scrollView.contentOffset.x / SCREEN_WIDTH;
    [self didClickedTitleButton:_titleView.subviews[index]];
}
- (void)didClickedTitleButton:(UIButton *)button {
    
    self.selectedButton.enabled = YES;
    button.enabled = NO;
    [UIView animateWithDuration:0.25 animations:^{
        self.lineView.width = button.titleLabel.width;
        self.lineView.centerX = button.centerX;
    }];
    self.selectedButton = button;
    NSInteger i = button.tag - 1000;
    CGPoint index = self.scrollView.contentOffset;
    index.x = i * SCREEN_WIDTH;
    [self.scrollView setContentOffset:index animated:YES];
}
- (void)createBarButton {
    
}
@end
